package lc.q101_150;

public class Q116 {

    public Node connect(Node root) {
        if (root == null) {
            return root;
        }
        if (root.left != null) {
            root.left.next = root.right;
        }
        if (root.right != null && root.next != null) {
            if (root.next.left != null) {
                root.right.next = root.next.left;
            } else {
                root.right.next = root.next.right;
            }
        }
        connect(root.left);
        connect(root.right);
        return root;
    }

}
